package com.aracky.common.annotation.dataconvert.user;

import com.aracky.common.annotation.DataDeal;
import com.aracky.common.mapper.BaseMapper;
import com.aracky.common.vo.UserVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zengzhiping
 * @ClassName UserUtil
 * @Description 用户转换表 --查询用户数据
 * @createTime 2022年06月13日
 */
@Component
@Slf4j
public class UserUtil {

    @Resource
    private BaseMapper baseMapper;

    @SneakyThrows
    public Boolean dealUser(List<UserDto> userParams, Map<String, String> userInfoMap, DataDeal annotation) {
        // 获取所有配置注解的code 和实体字段
        if (userParams.size() == 0 || !annotation.user()) {
            return Boolean.FALSE;
        }
        //通过用户id获取用户信息---分布式需要调用接口
        List<UserVO> userList = baseMapper.findUserList(getUserIdList(userParams));

        if (userList == null || userList.isEmpty()) {
            return Boolean.FALSE;
        }
        //先把用户信息转成map
        for (UserVO userVO : userList) {
            userInfoMap.put(userVO.getUserId() + "_" + userVO.getUserId(), userVO.getRealName());
        }
        return Boolean.TRUE;
    }

    /**
     * 获取实体中所有需要转换的用户信息
     *
     * @param userDtos
     * @return
     */
    private List<String> getUserIdList(List<UserDto> userDtos) {
        if (userDtos == null) {
            return new ArrayList<>();
        }
        List<String> collect = userDtos.stream()
                .map(dictDto -> dictDto.getUserId()).collect(Collectors.toList());
        collect.stream().distinct();
        return collect;
    }

}
